﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace Moose.Controls.Forms
{
    public partial class ProgressControlForm : Form
    {

        private ProgressControlForm Instance;
        private static readonly object locker = new object();

        private ProgressControlForm()
        {
            InitializeComponent();

            progressBar1.Value = 0;
            progressBar1.Step = 1;

            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ControlBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }

        private ProgressControlForm(string formName) : this()
        {
            this.Text = formName;
        }

        public ProgressControlForm GetInstace(string formName)
        {
            if (Instance == null)
            {
                lock (locker)
                {
                    if (Instance == null)
                    {
                        Instance = new ProgressControlForm(formName);
                    }
                }
            }
            return Instance;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Event_ProgressCancel?.Invoke();
            this.Close();
        }


        /// <summary>
        /// 取消发送
        /// </summary>
        public event Action Event_ProgressCancel;
        /// <summary>
        /// 完成发送
        /// </summary>
        public event Action Event_ProgressOver;

        public int MaxiMun
        {
            get
            {
                return progressBar1.Maximum;
            }
            set
            {
                if (progressBar1.InvokeRequired)
                {
                    progressBar1.Invoke(new Action(() =>
                    {
                        progressBar1.Maximum = value;
                    }));
                }
                else
                {
                    progressBar1.Maximum = value;
                }
            }
        }


        /// <summary>
        /// 移动进度条
        /// </summary>
        /// <param name="index"></param>
        public void MobileProgress(int index)
        {
            if (progressBar1.InvokeRequired)
            {
                progressBar1.Invoke(new Action<int>(MobileProgress), index);
            }
            else
            {
                progressBar1.PerformStep();

                Thread _MobileProgressTH = new Thread(new ThreadStart(() =>
               {
                   //进度条动画效果有延迟；
                   Thread.Sleep(500);
                   UpdateLabInfo(index);
               }));
                _MobileProgressTH.IsBackground = true;
                _MobileProgressTH.Start();
            }
        }

        /// <summary>
        /// 更新提示文本信息
        /// </summary>
        /// <param name="index"></param>
        private void UpdateLabInfo(int index)
        {
            if (labProgressInfo.InvokeRequired)
            {
                labProgressInfo?.Invoke(new Action<int>(UpdateLabInfo), index);
            }
            else
            {
                Application.DoEvents();
                labProgressInfo.Text = $"当前进度{((index * 1.0 / MaxiMun) * 100).ToString("F2")}%！！！";
                if (index == MaxiMun)
                {
                    if (MessageBox.Show(this, "当前操作已完成！！！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                    {
                        Event_ProgressOver?.Invoke();
                        this.Close();
                    }
                }
            }
        }

        public void MessageBoxInfo(string msg)
        {
            if (this.InvokeRequired)
            {
                this?.Invoke(new Action<string>(MessageBoxInfo), msg);
            }
            else
            {
                MessageBox.Show(this, "操作失败，异常信息：" + msg, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void CloseForm()
        {
            if (this.InvokeRequired)
            {
                this?.Invoke(new Action(CloseForm));
            }
            else
            {
                this.Close();
            }
        }
    }
}
